using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace tutFuelCellGame
{
    public class LevelCountDown
    {
        private readonly SpriteFont font;
        public float Scale;
        public float Opacity;
        public TimeSpan Timer;
        private Vector2 size;
        private bool hadFirstUpdate;


        public LevelCountDown(SpriteFont font)
        {
            this.font = font;
            hadFirstUpdate = false;
        }

        public void Update(TimeSpan elapsedSeconds)
        {
            if (!hadFirstUpdate)
            {
                Timer = TimeSpan.FromSeconds(4) + elapsedSeconds;
                hadFirstUpdate = true;
            }
            Timer -= elapsedSeconds;
        }

        public void Draw(SpriteBatch spriteBatch, Rectangle titleSafeArea)
        {
            // nothing to do if timer expired
            if (Timer < TimeSpan.Zero) 
                return;

            string s = (Timer.Seconds).ToString();
            if (s == "0") s = "GO!";
            var ds = font.MeasureString(s);
            size = ds*Scale;
            Vector2 strPosition = new Vector2(titleSafeArea.Center.X - size.X/2, titleSafeArea.Center.Y - size.Y/2);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            spriteBatch.DrawString(font, s, new Vector2(strPosition.X+2, strPosition.Y+2), new Color(Color.Black, Opacity), 0, Vector2.Zero, Scale, SpriteEffects.None, 0f);
            spriteBatch.DrawString(font, s, strPosition, new Color(Color.White, Opacity), 0, Vector2.Zero, Scale, SpriteEffects.None, 0f);
            spriteBatch.End();
            
        }

    }
}